<template>
    <div class="reg-box">
        <div class="nav">
            <span @click="$router.push('/login')">登录</span>
            <span class="register">注册</span>
        </div>
        <div class="reg">
            <div class="ipt-box">
                <img src="../../images/user.png"><input v-model="regObj.phone" type="text" placeholder="输入注册手机号">
            </div>
            <div class="ipt-box">
                <img src="../../images/pwd.png"><input v-model="regObj.name" type="text" placeholder="输入注册用户名">
            </div>
            <van-checkbox v-model="agree" checked-color="#FBD216">同意《用户协议》和《隐私政策》</van-checkbox>
            <van-button @click="regEvt"
            :disabled="!agree"
            :loading="loading"
            loading-text="注册中..."
            color="linear-gradient(to right, #FBD216, #ee0a24)"
            round
            block>注册</van-button>
        <span class="link" @click="$router.push('/login')">已有账号去登录</span>
        </div>

    </div>
</template>

<script>
//ee0a24
    import { registerApi } from '../../apis/loginApi'
    export default {
        data() {
            return {
                agree: false,
                regObj: {
                    phone: '',
                    name: ''
                },
                loading: false
            }
        },
        methods:{
            async regEvt(){
                if (!/^1[3-9]{1}[0-9]{9}$/.test(this.regObj.phone)){
                    this.$toast({
                        message:'请输入正确的手机号',
                        position:'bottom'
                    })
                    return
                }
                if (!this.regObj.name) {
                    this.$toast({
                        message:'用户名不能为空',
                        position:'bottom'
                    })
                    return
                }
                // 发起异步请求 -- 一定要注意二次点击，所以要把点击按钮遮掉，要用loading变量来实现一个节流
                if (this.loading) return
                // 需要把节流标识开启
                this.loading = true
                let result = await registerApi(this.regObj)
                console.log(result);
                if (result.code === 200) {
                    this.$toast.success('注册成功,请登录使用')
                    this.$router.push('/login')
                } else {
                    this.$toast.fail('注册失败,请重试')
                }

                // 节流标识应该等一下才关闭，避免用户点击比逻辑块
                setTimeout(() => {
                    this.loading = false
                })

            }
        }
    }
</script>


<style lang="less" scoped>
    @import "../../style/common.less";
    .reg-box {
        display: block;
        position: relative;
        height: 100%;
        padding: 4vh 20px 0;
    
        .nav {
                display: flex;
                > span {
                    margin: 30px 30px;
                    line-height: 30px;
                }
                .register {
                    font-size: 30px;
                    color: @primary;

                }
            }
        .reg {
            display: block;
            position: relative;
            height: 100%;
            padding: 4vh 20px 0;

        

            > .ipt-box {
                display: flex;
                padding: 10px 0;
                margin-bottom: 16px;
                border-bottom: solid 1px @cdd;
                input {
                    flex: 1;
                    height: 32px;
                    background: transparent;
                    border: none;
                    outline: none;
                    text-indent: 12px;
                    font-size: .8rem;
                    // & 与input是同级样式
                    &::-webkit-input-placeholder {
                        font-size: .75rem;
                        color: @cdd;
                    }
                }

                .btn {
                    flex: 0 0 126px;
                    // flex设置以后，width是不生效的
                    // width: 150px;
                    border-left: solid 1px @cdd;
                    font-size: .7rem;
                    line-height: 32px;
                    text-align: center;
                }

                > img{
                        width: 35px;
                        height: 35px;
                    }
            }

            .van-checkbox {
                margin-bottom: 16px;
                font-size: .75rem;
            }

            .link {
                display: block;
                margin: 16px 0;
                line-height: 32px;
                font-size: .75rem;
                color: @primary;
            }

            .right {
                position: absolute;
                left: 0;
                bottom: 16px;
                width: 100%;
                font-size: .65rem;
                color: @cdd;
                text-align: center;
            }
        }
    }    
</style>